import Vue from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'
import axios from './util/axios'
import ElementUI from 'element-ui'
import moveRouter from './router/moveRouter'
import 'element-ui/lib/theme-chalk/index.css'
import Router from 'vue-router'

//全局导出excel
import htmlToExcel from './excel/htmlToExcel';//刚刚写好的那个js文件的路径
Vue.prototype.htmlToExcels = htmlToExcel;

const originalPush = Router.prototype.push
Router.prototype.push = function push(location){
  return originalPush.call(this,location).catch(err=>err)
}
Vue.use(ElementUI)
Vue.prototype.$axios=axios
Vue.config.productionTip = false
router.beforeEach((to,from,next)=>{
  if (to.name === 'login') { //验证是不是登录进行放行
    next();
  }else{
    if (to.meta.reqiureAuth) { //是否有路由元信息
      if (sessionStorage.getItem('isLogin')) { // 验证用户是否登录
        if (store.getters.getisLogin && store.getters.getRefresh) {  //getRefresh判断是否刷新
          // 如果用户登录了，页面refresh值为true，则重新添加路由
          store.dispatch('setNoRefresh') // 重新刷新设置为false触发vuex
          moveRouter.DynamicAddRouter() // 添加动态路由的方法
          next({ ...to, replace: true })  //【必设置项】
        }else{
          next()
        }
      }else{
        console.log(sessionStorage.getItem('isLogin'))
        next({path:'/Login'})
      }
    }else if(sessionStorage.getItem('isLogin')){  //如果登录里面有状态
      if(store.getters.getRefresh){   //用户刷新时
        console.log('加载动态路由');
        store.dispatch('setNoRefresh') // 重新刷新设置为false
        moveRouter.DynamicAddRouter() // 添加动态路由的方法
        next({ ...to, replace: true }) //【必设置项】
      }else{
        next()
      }
    }
  }
  next()
})
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
